const express = require('express');
const expressWs = require('express-ws');
const router = express.Router()
expressWs(router);

const { GarageTable } = require('../sand_table/garage/GarageTable');


sandTable = new GarageTable();


router
    .ws('/status', function (ws, req) {
        ws.onmessage = ({ data: msg }) => {
            const { tag } = JSON.parse(msg)
            sandTable.endSendMessage()
            if (tag === "begin") {
                sandTable.storynum = 1
                console.log("begin");
            }
            else if (tag === "begin2"){
                sandTable.storynum = 2
                console.log("begin2");}
            sandTable.beginSendMessage()
            sandTable.addWs(ws)
        }


        ws.onclose = () => {
            sandTable.removeWs(ws)
            sandTable.endSendMessage()
            console.log("close");
        }
    })

// router.post("/lifter/status/story", function (req, res) {
//     sandTable.regularStory = new NightStory (sandTable)
// })

router.post("/lifter/status/set", function (req, res) {
    let { id, status } = req.body
    sandTable.changeLifterStatus(id, status)
    res.send({ code: 200 })
})

router.get("/lifter/up/:id", function (req, res) {
    let id = req.params.id
    sandTable.upLifter(id)
    res.send({ code: 200 })
})

router.get("/lifter/down/:id", function (req, res) {
    let id = req.params.id
    sandTable.downLifter(id)
    res.send({ code: 200 })
})

module.exports = router
// module.exports.getSharedData = () => sharedData;